package dabs.engine.plant;

import dabs.engine.control.ComponentCommand;
import dabs.engine.environment.AcceptsModifier;
import dabs.engine.environment.Modifier;
import dabs.engine.environment.TemporaryDisable;
import dabs.engine.messaging.Notification;
import dabs.engine.messaging.NotificationHandler;

public class Pump extends ComponentPart implements AcceptsModifier{
	private static final long serialVersionUID = 7244797591801230882L;
	
	private int flowAmount;
	
	public Pump(NotificationHandler mailBox, String name) {
		super(mailBox, name);
		flowAmount = 3;
	}
	
	public int getFlowAmount() {
		return flowAmount;
	}
	
	public void setFlowAmount( int flowAmount ) { 
		if ( isDeactivated() == false ) {
			this.flowAmount = flowAmount;
			passMessage(new Notification(getName(), "Speed set to " + flowAmount + "%."));
		} 
		else { 
			passMessage( new Notification(getName(), "Pump is stalled") );
		}
	}
	
	@Override
	protected void inactivatedModifier(Modifier modifier) {
		super.inactivatedModifier(modifier);
		if (modifier instanceof TemporaryDisable) {
			passMessage(new Notification(getName(), "is now active again"));
		}
	}
	
	@Override
	public void tick() {
		getNewHydroState().sendToNext(flowAmount, true, getOutputs());
		super.tick();
	}
	
	@Override
	public void runCommand(ComponentCommand command) {
		assert(command.getDestination().equals(getName()));
		int newSpeed = Integer.parseInt(command.getArguments().get(0)); //Line to be changed depending on arguments.
		if (newSpeed <= 100 && newSpeed >= 0){
			setFlowAmount(newSpeed);
		}
		else{
			passMessage(new Notification(getName(), "Speed  must be set to a value between 0 and 100"));
		}
	}
	
	
}
